package broker

import (
  "context"
  "gitlab.local/backend/proto/dto"
  "manager/bll"
  log "manager/collector/logger"
  "manager/event"
  SignalPo "manager/model/signal"
)

var CommandHandler = commandHandler{}

type commandHandler struct {
}

func (d *commandHandler) init() func() {
  return func() {}
}

func (d *commandHandler) onEvent(data *event.Data) {
  if data == nil || event.Event_Command_Report != data.Type {
    return
  }

  obj := data.Data.(*event.CommandReport)
  if obj == nil {
    log.Error("commandHandler onEvent err")
    return
  }

  switch obj.Cmd {
  // 锁定
  case dto.EventType_Lock:
    // 下电
    log.Infof("commandHandler onEvent EventType_Lock ebikeId:%v", obj.EbikeId)
    err := bll.Signal.SendCmdToDevice(context.Background(), &SignalPo.CommandReq{
      EbikeId: obj.EbikeId,
      Cmd:     int(dto.CmdType_CmdPowerOff),
    })

    if err != nil {
      log.Errorf("commandHandler onEvent err:%v", err)
      return
    }
  // 解锁
  case dto.EventType_Unlock:

  }
}
